package com.hs.authcenter.service;

import com.hs.authcenter.entity.User;
import com.hs.authcenter.entity.UserDetailsWrap;
import com.hs.authcenter.feign.UserFeignService;
import com.hs.common.api.CommonResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Component;

/**
 * @Description: TODO
 * @Author 胡尚
 * @Date: 2024/7/26 10:01
 */
@Slf4j
@Component
public class HushangUserDetailsService implements UserDetailsService {

    @Autowired
    private UserFeignService userFeignService;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {

        // 通过OpenFeign 远程调用user微服务获取用户相关的信息
        CommonResult<User> commonResult = userFeignService.queryUser(username);
        User user = commonResult.getData();

        if (user == null) {
            return null;
        }


        // 对user进行一个封装
        return new UserDetailsWrap(user);
    }
}
